//
//  CycleScrollVIew.m
//  UIScrollVIewHomework
//
//  Created by lanou on 16/1/30.
//  Copyright © 2016年 Dangerous. All rights reserved.
//

#import "CycleScrollVIew.h"
#import "NSTimer+Addition.h"

#define  kWidth self.frame.size.width
#define  kHight self.frame.size.hight


@interface CycleScrollVIew ()<UIScrollViewDelegate>
{
    NSInteger _imagesCount; // 记录传入的数组元素的个数
    NSTimeInterval _animationDuration; // 定义自动滚动的时间间隔
}
@property (nonatomic, retain) NSArray *imagesArray; // 接受传入的图片数组
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSTimer *timer; //使用NSTimer让图片自动滚动

@end

@implementation CycleScrollVIew



- (instancetype)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray animationDuration:(NSTimeInterval)animationDuration
{
    self = [super initWithFrame:frame];
    if (self) {
        _imagesArray = [imagesArray retain];
        _imagesCount = imagesArray.count;
        // 初始化UIScrollView对象
        [self createScrollView];
        // 初始化NSTimer对象，并且让它进行滚动,如果给的时间间隔为0的话，那么不能自动滚动
        if (animationDuration != 0) {

        _timer = [NSTimer scheduledTimerWithTimeInterval:animationDuration target:self selector:@selector(autoChangeFrame:) userInfo:nil repeats:YES];
        }
        
        
//        _imagesArray = [imagesArray retain];
//        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
//        _scrollView.contentSize = CGSizeMake(kWidth * (_imagesArray.count + 2), kHeight);
//        _scrollView.showsHorizontalScrollIndicator = NO;
//        _scrollView.contentOffset = CGPointMake(kWidth, 0);
//
//        _scrollView.pagingEnabled = YES;
//        
//        _scrollView.delegate = self;
//
//        for (int i = 0; i < _imagesArray.count + 2; i++) {
//            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight)];
//            if (i == 0) {
//                imageView.image = [UIImage imageNamed:_imagesArray[_imagesArray.count - 1]];
//            } else if (i == _imagesArray.count + 1) {
//                imageView.image = [UIImage imageNamed:_imagesArray[0]];
//            } else {
//                imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", _imagesArray[i - 1]]];
//            }
//            [_scrollView addSubview:imageView];
//            [imageView release];
//        }
//        [self addSubview:_scrollView];
    }
    return self;
}

- (void)autoChangeFrame:(NSTimer *)timer
{
    CGPoint newOffset = CGPointMake(_scrollView.contentOffset.x + kWidth, 0);
    [_scrollView setContentOffset:newOffset animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [_timer resumeTimerAfterTimeInterval:_animationDuration];
}

#pragma mark - UIScrollView的初始化
- (void)createScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _scrollView.contentSize = CGSizeMake((_imagesCount + 2) * kWidth, 0);
    // 隐藏水平滚动条
    _scrollView.showsHorizontalScrollIndicator = NO;
    // 设置一页滚动一次
    _scrollView.pagingEnabled = YES;
    // 设置偏移量
    _scrollView.contentOffset = CGPointMake(kWidth, 0);
    // 设置代理delegate为当前对象
    _scrollView.delegate = self;
    // 循环设置图片
    [self createImageViews];
    // 把scrollView添加到self上
    [self addSubview:_scrollView];
}


#pragma mark - 循环设置UIImageView
- (void)createImageViews
{
    for (int i = 0; i < _imagesCount + 2; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight)];
        if (i == 0) {
            imageView.image = [_imagesArray lastObject];
        } else if (i == _imagesCount + 1){
            imageView.image = [_imagesArray firstObject];
        } else {
            imageView.image = _imagesArray[i - 1];
        }
        [_scrollView addSubview:imageView];
        [imageView release];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 首先得到当前的偏移量
    CGPoint currentPoint = scrollView.contentOffset;
    if (currentPoint.x <= kWidth / 2) {
        scrollView.contentOffset = CGPointMake(_imagesCount * kWidth  + currentPoint.x, 0);
    } else if (currentPoint.x > (_imagesCount + 0.5) * kWidth){
        scrollView.contentOffset = CGPointMake(currentPoint.x - _imagesCount * kWidth, 0);
        
    }
}

- (void)dealloc
{
    [_scrollView release];
    [_imagesArray release];
    [_timer release];
    [super dealloc];
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.x / kWidth == 0) {
//        scrollView.contentOffset = CGPointMake(kWidth * _imagesArray.count - 2, 0);
//        
//    } else if (scrollView.contentOffset.x / kWidth == _imagesArray.count - 1) {
//        scrollView.contentOffset = CGPointMake(kWidth, 0);
//    }
//}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
